<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>招商银行网点管理系统</title>
        <meta name="description" content="招商银行">
        <meta name="keywords" content="招商银行">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="plugins/elementui/index.css">
        <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <!--第一行：分隔线上方的招商银行网点管理系统首页-->
            <div class="content-header">
                <h1>招商银行<small>网点管理系统</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                </el-breadcrumb>
            </div>

            <div class="app-container">
                <div class="box">
                    <!--第二行：搜索下拉列表、查询按钮、新建按钮-->
                    <div class="filter-container">
                        <el-select v-model="areaid" placeholder="请选择城市">
                            <el-option :label="'请选择城市'" :key="-1" :value="-1"></el-option>
                            <el-option
                                    v-for="item in options"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                        <el-button @click="bankList()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        <el-button plain  onclick="location.href='operatorLog.html'">查看日志</el-button>
                    </div>

                    <!--第三行：表格展示银行网点数据-->
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="网点名称" align="center"></el-table-column>
                        <el-table-column prop="areaName" label="城市" align="center"></el-table-column>

                        <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                        <el-table-column prop="telephone" label="电话" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增网点" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="addFormData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="网点名称" prop="name">
                                            <el-input v-model="addFormData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="所属城市" prop="areaid">
                                            <el-select v-model="addFormData.areaid" placeholder="请选择城市">
                                                <el-option
                                                        v-for="item in options"
                                                        :key="item.id"
                                                        :label="item.name"
                                                        :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="详细地址" prop="address">
                                            <el-input v-model="addFormData.address"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="电话号码" prop="telephone">
                                            <el-input v-model="addFormData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>


                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑网点" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="editFormData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="网点名称" prop="name">
                                            <el-input v-model="editFormData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="所属城市" prop="areaid">
                                            <el-select v-model="editFormData.areaid" placeholder="请选择城市">
                                                <el-option
                                                        v-for="item in options"
                                                        :key="item.id"
                                                        :label="item.name"
                                                        :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="详细地址" prop="address">
                                            <el-input v-model="editFormData.address"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="电话号码" prop="telephone">
                                            <el-input v-model="editFormData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>


                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="js/vue.js"></script>
    <script src="plugins/elementui/index.js"></script>
    <script src="js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                areaid:-1,//查询条件城市id,-1表示没有选择城市
				dataList: [],//当前页要展示的分页列表数据
                addFormData: {},//新增表单数据
                editFormData: {},//编辑表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                options:[],  //显示城市下拉列表
                rules: {//校验规则
                    name: [{ required: true, message: '网点名称为必填项', trigger: 'blur' }],
                    address: [{ required: true, message: '地址为必填项', trigger: 'blur' }],
                    areaid: [{ required: true, message: '城市为必先项', trigger: 'blur' }],
                    telephone: [{ required: true, message: '电话为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            mounted() {
                this.bankList();//VUE对象初始化完成后调用查询所有方法，完成查询所有
                this.cityList();  //查询城市列表
            },
            methods: {
                //查询所有城市列表
                cityList(){
                    axios.get("area").then((resp)=>{
                        //给下拉列表模型数据赋值
                        if(resp.data.code===1){
                            this.options=resp.data.data;
                        }
                    })
                },
                //查询所有银行网点
                bankList() {
                    //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                    axios.get("bank/list/"+this.areaid).then((resp)=>{
                        //解析服务器响应回的数据，为模型数据赋值
                        if(resp.data.code===1){
                            this.dataList = resp.data.data;
                        }
                    });
                },
                // 弹出添加窗口
                handleCreate() {
                    //弹出新增窗口
                    this.dialogFormVisible = true;
                    this.resetForm();
                },
                //添加银行网点
                handleAdd () {
                    //进行表单校验
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                            console.log(this.addFormData);
                            axios.post("bank",this.addFormData).then((resp) => {
                                if(resp.data.code===1){
                                    //关闭新增窗口
                                    this.dialogFormVisible = false;
                                    //新增成功后，重新调用查询所有方法，查询出最新的数据
                                    this.bankList();
                                    //弹出提示信息
                                    this.$message({
                                        message:resp.data.msg,
                                        type:'success'
                                    });
                                }
                            });
                        }else{
                            //校验不通过
                            this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                            return false;
                        }
                    });
                },
                // 弹出编辑窗口，回显要修改的银行网点信息
                handleUpdate(row) {
                    //弹出编辑窗口
                    this.dialogFormVisible4Edit = true;
                    //回显数据，发送ajax请求根据ID查询当前检查项数据
                    axios.get("bank/getById/" + row.id).then((resp) => {
                        //进行回显，基于VUE的数据绑定实现
                        if(resp.data.code===1){
                            this.editFormData = resp.data.data;
                        }
                    });
                },
                //编辑/修改银行网点
                handleEdit() {
                    //进行表单校验
                    this.$refs['dataEditForm'].validate((valid) => {
                        if(valid){
                            //表单校验通过，可以提交数据
                            axios.put("bank",this.editFormData).then((resp) => {
                                if(resp.data.code===1){
                                    //弹出成功提示信息
                                    this.$message({
                                        type:'success',
                                        message:resp.data.msg
                                    });
                                    //不管成功还是失败，都调用查询所有方法
                                    this.bankList();
                                    //隐藏编辑窗口
                                    this.dialogFormVisible4Edit = false;
                                }
                            })
                        }else{
                            //表单校验不通过
                            this.$message.error("表单数据校验失败!");
                            return false;
                        }
                    });
                },
                // 删除银行网点
                handleDelete(row) {
                    this.$confirm("你确定要删除当前数据吗？","提示",{//确认框
                        type:'warning'
                    }).then(()=>{
                        //用户点击确定按钮，发送ajax请求，将检查项ID提交到服务器进行处理
                        axios.delete("bank/" + row.id).then((resp) => {
                            if(resp.data.code===1){
                                //弹出成功提示信息
                                this.$message({
                                    type:'success',
                                    message:resp.data.msg
                                });
                                //重新进行查询所有
                                this.bankList();
                            }
                        });
                    }).catch(()=>{
                        this.$message({
                            type:'info',
                            message:'操作已取消'
                        });
                    });
                },
                // 重置表单
                resetForm() {
                    this.addFormData = {};//重置数据
                    this.editFormData = {};//重置数据
                }
            }
        })
    </script>
</html>